
import axios from 'axios'

// 设置基准路径
axios.defaults.baseURL = 'http://localhost:8888/api/private/v1/'

axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  // 先获取token
  var token = localStorage.getItem('heima_manerger_token')
  if (token) {
    // 2.必须在请求头中使用 Authorization 字段提供 token 令牌
    config.headers['Authorization'] = token
  }
  // console.log(config)

  return config
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error)
})
// 实现用户登陆
export const login = (data) => {
  console.log(data)
  return axios({
    method: 'post',
    url: 'login',
    data
  })
}

export const getUserList = (pa) => {
  return axios({
    url: 'users',
    params: pa
  })
}
export const addUser = (data) => {
  console.log(data)
  return axios({
    url: 'users',
    method: 'post',
    data
  })
}
// 根据用户id删除
export const delUser = (id) => {
  return axios({
    method: 'delete',
    url: `users/${id}`
  })
}
// 添加修改状态的接口
export const statusChange = (uid, type) => {
  return axios({
    method: 'put',
    url: `users/${uid}/state/${type}`
  })
}
// 分配角色的接口
export const grantUserRole = (id, rid) => {
  return axios({
    method: 'put',
    url: `users/${id}/role`,
    data: { rid }
  })
}

export const editUser = (data) => {
  return axios({
    method: 'put',
    url: `users/${data.id}`,
    data: { email: data.email, mobile: data.mobile, username: data.username }
  })
}
